package 笔记.多线程;


import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * @author CHL
 * @ClassName: CyclickBarrierDemo
 * @Description: 多线程 加法计数器的运用
 * @date 2023/11/3 14:32
 * @Version 1.0
 */
public class CyclickBarrierDemo {

    public static void main(String[] args) {


        /*
        * CyclicBarrier 可以循环使用
        *
        *
        * */
        CyclicBarrier cyclickBarrier = new CyclicBarrier(8,()->{
            System.out.println(" 集齐8个召唤神龙:");
        });

        for (int i = 0; i < 8; i++) {
            int finalI = i;
            new Thread(() -> {

                System.out.println(Thread.currentThread().getName() + "第"+finalI+"颗龙珠");
                try {
                    cyclickBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            },"线程"+i).start();

        }


    }
}
